<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 4/22/24
 * Time: 3:03 PM
 */

namespace app\adminapi\logic\media;

use app\common\model\media\Media;
use app\common\logic\BaseLogic;
use app\common\model\media\MediaAuth;


/**
 * MediaAccount逻辑
 * Class MediaAccountLogic
 * @package app\adminapi\logic
 */
class MediaAuthLogic extends BaseLogic
{

    /**
     * 获取所有的授权媒体应用
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function all()
    {
        $list = MediaAuth::with([
            'media' => function ($q) {
                $q->withField('id, name');
            }
        ])->field('id, media_id, auth_name')->select();

        $result = [];
        foreach ($list as $item) {
            $result[] = [
                'id'   => $item->id,
                'name' => ($item->media->name ?? '') . ' - ' . $item->auth_name
            ];
        }

        return $result;
    }


    /**
     * 添加
     *
     * @param $params
     * @return bool
     */
    public static function add($params)
    {
        $mediaId    = $params['media_id'] ?? 0;
        $name       = $params['name'] ?? '';
        $appId      = $params['app_id'] ?? '';
        $secret     = $params['secret'] ?? '';
        $username   = $params['username'] ?? '';
        $password   = $params['password'] ?? '';
        $token      = $params['token'] ?? '';
        $platformId = $params['platform_id'] ?? '';
        // $scope      = $params['scope'] ?? '';
        $time = time();

        MediaAuth::create([
            'media_id'    => $mediaId,
            'admin_id'    => $params['admin_id'] ?? 0,
            'auth_name'   => $name,
            'app_id'      => $appId,
            'secret'      => $secret,
            'username'    => $username,
            'password'    => $password,
            'token'       => $token,
            'platform_id' => $platformId,
            'scope'       => $mediaId == Media::SOURCE_KS ? 'ad_query,ad_manage,report_service,account_service' : '',
            'status'      => 1,
            'create_time' => $time,
            'update_time' => $time
        ]);

        return true;
    }


    /**
     * 编辑
     *
     * @param $params
     * @return bool
     */
    public static function edit($params)
    {
        $id = $params['id'] ?? 0;

        $mediaAuth = MediaAuth::where('id', $id)->findOrEmpty();
        if ($mediaAuth->isEmpty()) {

            return false;
        }

        $mediaId    = $params['media_id'] ?? 0;
        $name       = $params['name'] ?? '';
        $appId      = $params['app_id'] ?? '';
        $secret     = $params['secret'] ?? '';
        $username   = $params['username'] ?? '';
        $password   = $params['password'] ?? '';
        $token      = $params['token'] ?? '';
        $platformId = $params['platform_id'] ?? '';
        $time       = time();

        $mediaAuth->media_id    = $mediaId;
        $mediaAuth->auth_name   = $name;
        $mediaAuth->app_id      = $appId;
        $mediaAuth->secret      = $secret;
        $mediaAuth->username    = $username;
        $mediaAuth->password    = $password;
        $mediaAuth->token       = $token;
        $mediaAuth->platform_id = $platformId;
        $mediaAuth->update_time = $time;

        return $mediaAuth->save();
    }


    /**
     * 获取添加应用字段
     * @return array[]
     */
    public static function getMediaType()
    {
        return [
            [
                'media_id' => Media::SOURCE_JL,
                'name'     => '请输入巨量应用名称',
                'app_id'   => '请输入巨量应用app_id',
                'secret'   => '请输入巨量应用secret',
            ],
            [
                'media_id' => Media::SOURCE_UC,
                'name'     => '请输入UC应用名称',
                'username' => '请输入UC管家账户',
                'password' => '请输入密码',
                'token'    => '请输入token'
            ],
            [
                'media_id' => Media::SOURCE_KS,
                'name'     => '请输入快手应用名称',
                'app_id'   => '请输入快手应用app_id',
                'secret'   => '请输入快手应用secret',
                // 'scope'    => '请输入快手应用scope'
            ],
            [
                'media_id'    => Media::SOURCE_BD,
                'name'        => '请输入百度应用名称',
                'app_id'      => '请输入百度应用app_id',
                'secret'      => '请输入百度应用secret',
                'platform_id' => '请输入百度应用platform id'
            ],
            [
                'media_id' => Media::SOURCE_OPPO,
                'name'     => '请输入OPPO应用名称',
                'app_id'   => '请输入OPPO应用API-ID',
                'secret'   => '请输入OPPO应用API-KEY',
            ],
            [
                'media_id' => Media::SOURCE_VIVO,
                'name'     => '请输入VIVO应用名称',
                'app_id'   => '请输入VIVO应用clientId',
                'secret'   => '请输入VIVO应用secret',
            ],
        ];
    }
}
